package cn.cloud9.opencloud.common.exception;

import cn.cloud9.opencloud.common.api.ResultMessage;

import java.io.Serializable;
import java.text.MessageFormat;

/**
 * 服务异常类
 * @author OnCloud9
 * @description
 * @project Open-Cloud
 * @date 2022年04月16日 11:49
 */
public class ServiceException extends RuntimeException implements Serializable {

    private static final long serialVersionUID = -9092155973005166304L;
    private ResultMessage resultMessage;
    private Object[] arguments;

    /**
     * 默认直接系统异常
     */
    public ServiceException() {
        super(ResultMessage.SYSTEM_ERROR.getMessage());
        this.resultMessage = ResultMessage.SYSTEM_ERROR;
    }

    /**
     * 注入默认的枚举项响应信息
     * @param resultMessage
     */
    public ServiceException(ResultMessage resultMessage) {
        super(resultMessage.getMessage());
        this.resultMessage = resultMessage;
    }

    /**
     * 注入默认的枚举项响应信息
     * @param resultMessage
     */
    public ServiceException(ResultMessage resultMessage, Object ... arguments) {
        super(MessageFormat.format(resultMessage.getMessage(), arguments));
        this.resultMessage = resultMessage;
    }


    /**
     * 需要指定的异常
     * @param throwable
     * @param resultMessage
     */
    public ServiceException(Throwable throwable, ResultMessage resultMessage) {
        super(resultMessage.getMessage(), throwable);
        this.resultMessage = resultMessage;
    }

    /**
     * 需要指定的异常, 附带解释参数
     * @param throwable
     * @param resultMessage
     */
    public ServiceException(Throwable throwable, ResultMessage resultMessage, Object ... arguments) {
        super(MessageFormat.format(resultMessage.getMessage(), arguments), throwable);
        this.resultMessage = resultMessage;
    }

    public ResultMessage getResultMessage() {
        return resultMessage;
    }

    public Object[] getArguments() {
        return arguments;
    }
}
